package com.smzd.ccms.complets;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.smzd.ccms.business.ArticleService;
import com.smzd.ccms.core.BaseCompletPlugin;
import com.smzd.ccms.core.ParamsWrapper;
import com.smzd.ccms.domain.Article;

/**
 * 获取指定文章的组件<br>
 * <br>
 * 该组件优先从URL中获取路径变量中获取，如果没有则从URL参数列表中取名字为id的参数值；如果仍然没有，则看是否为本组件提供名字为id的参数配置。
 * 如果有，则返回对于的文章，否则返回null。
 * 
 * @author YuZhongda
 * 
 */
public class ArticleComplet extends BaseCompletPlugin {
	ArticleService articleService;

	public ArticleComplet(int id) {
		super(id);
		articleService = ArticleService.getInstance();
	}

	@Override
	public String execute(ParamsWrapper params) throws Exception {
		if (articleService == null) {
			synchronized (this) {
				if (articleService == null) {
					articleService = ArticleService.getInstance();
				}
			}
		}
		Integer id = params.getParamValue("id", 0);
		if (id == null || id.intValue() == 0) {
			return null;
		}
		Article article = articleService.getItemById(id);
		try {
			article.setContent(URLDecoder.decode(article.getContent(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		params.put("article", article);
		return params.getParamValue("view", "ch-art.ftl");
	}

}
